package com.zuipin.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zuipin.entity.SysResource;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.Result;
import com.zuipin.mapper.SysResourceMapper;
import com.zuipin.service.ISysResourceService;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;

/**
 * 资源(按纽/功能)服务实现
 * 
 * @ClassName: SysResourceSer5viceImpl
 * @Description: TODO
 * @author  robot
 * @date 2017年09月147日 09:21:09
 */
@Service
public class SysResourceServiceImpl implements ISysResourceService {
	
	@Resource
	private SysResourceMapper sysResourceMapper;
	
	@Override
	public SysResource findById(Long id) {
		return sysResourceMapper.findById(id);
	}
	
	@Override
	public SysResource findByCode(String resourceCode, Long sysId) {
		// TODO Auto-generated method stub
		return sysResourceMapper.findByCode(resourceCode, sysId);
		
	}
	
	@Override
	public void update(SysResource sysResource) {
		sysResource.setUpdateTime(DateUtils.getCurrentDateTime());
		sysResourceMapper.update(sysResource);
	}
	
	@Override
	public void delete(Long id) {
		sysResourceMapper.delete(id);
		
	}
	
	@Override
	public List<SysResource> seachSysResource(SysResource sysResource, Pagination page) {
		return sysResourceMapper.seachSysResource(sysResource, page);
	}
	
	@Override
	public List<SysResource> findByRoleIdAndMenuId(Long roleId, Long menuId, Long sysId) {
		// TODO Auto-generated method stub
		return sysResourceMapper.findByRoleIdAndMenuId(roleId, menuId, sysId);
	}
	
	@Override
	public void insert(SysResource sysResource) {
		sysResource.setResourceStatus(SysResource.ENABLE);
		if (sysResource.getIsPublic() == null) {
			sysResource.setIsPublic(SysResource.NOT_PUBLIC);
		}
		sysResource.setCreateTime(DateUtils.getCurrentDateTime());
		sysResource.setUpdateTime(DateUtils.getCurrentDateTime());
		sysResource.setDelFlag(0);
		sysResourceMapper.insert(sysResource);
	}
	
	/**
	 * 
	 * 后台新增
	 * 
	 */
	@Override
	public Result<SysResource> insertBack(SysResource sysResource) {
		// 判断CODE是否被使用
		Result<SysResource> result = new Result<SysResource>();
		SysResource parameter = new SysResource();
		parameter.setDelFlag(0);
		parameter.setResourceCode(sysResource.getResourceCode());
		List<SysResource> rs = seachSysResource(parameter, new Pagination());
		if (rs != null && !rs.isEmpty()) {
			result.setData(sysResource);
			result.setErrorCode(ErrorCode.OBJECT_EXIST);
			result.setMessage("添加失败,资源编号已经存在");
			return result;
		}
		insert(sysResource);
		result.setData(sysResource);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("新增成功");
		return result;
	}
	
	/**
	 * 
	 * 后台新增
	 * 
	 */
	@Override
	public Result updateBack(SysResource sysResource) {
		Result result = new Result();
		sysResource.setUpdateTime(DateUtils.getCurrentDateTime());
		sysResourceMapper.updateBack(sysResource);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("修改成功");
		return result;
	}
	
}
